/** 
 * https://leetcode.cn/problems/length-of-longest-subarray-with-at-most-k-frequency/submissions/560558816/
 * 2958. 最多 K 个重复元素的最长子数组
 * medium,  2024.9.2
 * 滑动窗口
 */

class Solution {
public:
    int maxSubarrayLength(vector<int>& nums,int k) {
        int res=0;
        unordered_map<int,int> cnt;
        int j=0;
        for (int i=0;i<nums.size();i++) {
            cnt[nums[i]]++;
            while (cnt[nums[i]]>k) {
                cnt[nums[j++]]--;
            }
            res=max(res,i-j+1);
        }
        return res;
    }
};

